package com.noble.library.camera;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.widget.Toast;

import com.noble.library.camera.util.Rx;

import java.io.InputStream;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * 相机相册对外开放工具类: [https://github.com/TF27674569/RxPhoto]
 * <p>
 * 须在调用相机相册的 Activity.onCreate() 中的 setContentView 下添加以下代码，
 * 用来屏蔽7.0中使用 Uri.fromFile爆出的FileUriExposureException
 * StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
 * StrictMode.setVmPolicy(builder.build());
 * if (Build.VERSION.SDK_INT >= 24) {
 * builder.detectFileUriExposure();
 * }
 */
public class ImgSelectUtil {
    private static ImgSelectUtil mInstance = null;

    public static ImgSelectUtil getInstance() {
        if (mInstance == null) {
            mInstance = new ImgSelectUtil();
        }
        return mInstance;
    }

    /**
     * 拍照，有裁切
     */
    @SuppressLint("CheckResult")
    public void takePhoto(final Activity activity, final OnChooseCompleteListener completeListener) {
        //成功后跳转相机
        // 相机拍时候跳转裁剪
        // 裁剪后 uri转bitmap
        Rx.create(activity)
                .permission()
                .permission(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request()
                .filter(aBoolean -> { // 判断权限是否授予陈宫
                    if (!aBoolean)
                        Toast.makeText(activity, "您还未授予相应权限", Toast.LENGTH_SHORT).show();
                    return aBoolean;
                })
                .flatMap((Function<Boolean, ObservableSource<Uri>>) aBoolean -> Rx
                        .create(activity)
                        .camera()
                        .obsrver())
                .flatMap((Function<Uri, ObservableSource<Uri>>) uri -> Rx
                        .create(activity)
                        .zoom(uri)
                        .obsrver())
                .map(uri -> {
                    if (completeListener != null) {
                        completeListener.onUrl(uri.getPath());
                    }
                    InputStream input = activity.getContentResolver().openInputStream(uri);
                    Bitmap bitmap = BitmapFactory.decodeStream(input);
                    input.close();
                    return bitmap;
                })
                .subscribe(bitmap -> {
                });
    }

    /**
     * 相册，有裁切
     */
    @SuppressLint("CheckResult")
    public void pickPhoto(final Activity activity, final OnChooseCompleteListener completeListener) {
        //成功后跳转相册
        // 相册跳转裁剪
        // 裁剪后 uri转bitmap
        Rx.create(activity)
                .permission()
                .permission(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request()
                .filter(aBoolean -> { // 判断权限是否授予陈宫
                    if (!aBoolean)
                        Toast.makeText(activity, "您还未授予相应权限", Toast.LENGTH_SHORT).show();
                    return aBoolean;
                })
                .flatMap((Function<Boolean, ObservableSource<Uri>>) aBoolean -> Rx
                        .create(activity)
                        .picture()
                        .obsrver())
                .flatMap((Function<Uri, ObservableSource<Uri>>) uri -> Rx
                        .create(activity)
                        .zoom(uri)
                        .obsrver())
                .map(uri -> {
                    if (completeListener != null) {
                        completeListener.onUrl(uri.getPath());
                    }
                    InputStream input = activity.getContentResolver().openInputStream(uri);
                    Bitmap bitmap = BitmapFactory.decodeStream(input);
                    input.close();
                    return bitmap;
                })
                .subscribe(bitmap -> {
                });
    }

    /**
     * 录像
     */
    @SuppressLint("CheckResult")
    public void takeVideo(final Activity activity, final OnChooseCompleteListener completeListener) {
        //成功后跳转相机
        Rx.create(activity)
                .permission()
                .permission(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request()
                .filter(aBoolean -> { // 判断权限是否授予陈宫
                    if (!aBoolean)
                        Toast.makeText(activity, "您还未授予相应权限", Toast.LENGTH_SHORT).show();
                    return aBoolean;
                })
                .flatMap((Function<Boolean, ObservableSource<Uri>>) aBoolean -> Rx
                        .create(activity)
                        .record()
                        .obsrver())
                .map((Function<Uri, Object>) uri -> {
                    if (completeListener != null) {
                        completeListener.onUrl(uri.getPath());
                    }
                    return uri;
                })
                .subscribe();
    }


    /**
     * 相册选择照片完成监听接口
     */
    public interface OnChooseCompleteListener {
        void onUrl(String url);
    }
}
